package duppentity.dto.sys;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import utils.JSONObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 将dupp返回的数据重新进行封装
 * Author: pengpeng@djcars.cn
 * Date: 2019/11/18 11:56
 */

public class SysHelpDtoUtil {

    public static List<SysHelpDto> getSysHelpDtoList(JsonObject data){
        List<SysHelpDto> sysHelpDtoList = null;
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        sysHelpDtoList = new ArrayList<>();
                        for (Integer i=0;i<list.size();i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            SysHelpDto sysHelpDto = convertSysHelpDto(listOne, data);
                            sysHelpDtoList.add(sysHelpDto);
                        }
                    }
                }
            }
        }
        return  sysHelpDtoList;
    }

    public static SysHelpDto getSysHelpDto(JsonObject data) {
        SysHelpDto sysHelpDto =new SysHelpDto();
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    JSONObject listOne = JSONObjectUtils.get(list, 0);
                    sysHelpDto = convertSysHelpDto(listOne, data);
                }
            }
        }
        return sysHelpDto;
    }

    private static SysHelpDto convertSysHelpDto(JSONObject listOne, JsonObject data){
        SysHelpDto sysHelpDto = new SysHelpDto();
        sysHelpDto.id = listOne.getIntValue("id");
        sysHelpDto.content = listOne.getString("content");
        sysHelpDto.title = listOne.getString("title");
        sysHelpDto.orderIndex = listOne.getIntValue("orderIndex");
        sysHelpDto.createTime = listOne.getLongValue("createTime");
        sysHelpDto.visual = listOne.getIntValue("visual");
        sysHelpDto.type = listOne.getIntValue("type");
        sysHelpDto.createBy = listOne.getLongValue("createBy");
        if (sysHelpDto.createBy > 0) {
            JSONObject sysAdmin = JSONObjectUtils.get(data, "sys_admin");
            sysHelpDto.name = JSONObjectUtils.getString(sysAdmin, sysHelpDto.createBy.toString(), "name");
        }
        return  sysHelpDto;
    }
}

